<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class course extends CI_Controller {
	public function read($cat='', $scat='')
	{
            $this->load->model('query_db');
            
            $data['sms_error']='';
            
            $data['show_menu']='0';
            if($cat==''&&$scat=='')
                $data['show_menu']='1';

            $data['category']=$this->query_db->CATEGORY_SELECT_ALL();
            $data['sub_category']=$this->query_db->SUB_CATEGORY_SELECT_ALL();
            
            if($cat!='')
            {
                if($this->in_array_field($cat, 'CATEGORY_ID', $data['category']))
                {
                    $data['category_info']=array_merge($this->filter_by_value($data['category'],'CATEGORY_ID',$cat));

                    if($this->in_array_field($cat, 'CATEGORY_ID', $data['sub_category']))
                    {
                        $data['sub_category_info']=array_merge($this->filter_by_value($data['sub_category'],'CATEGORY_ID',$cat));
                        
                        if($scat!='')
                        {
                            if(!$this->in_array_field($scat, 'SUB_CATEGORY_ID', $data['sub_category_info']))
                                show_404();
                            else 
                                $data['sub_category_id']=$scat;
                        }
                    }
                    else
                        show_404();
                }
                else
                    show_404();
            }
            
            $this->load->view('course',$data);
	}
        
        function filter_by_value ($array, $index, $value)
        { 
            if(is_array($array) && count($array)>0)  
            { 
                foreach(array_keys($array) as $key){ 
                    $temp[$key] = $array[$key][$index]; 

                    if ($temp[$key] == $value){ 
                        $newarray[$key] = $array[$key]; 
                    } 
                } 
              } 
          return $newarray; 
        }   
        function in_array_field($needle, $needle_field, $haystack, $strict = false) { 
            if ($strict) { 
                foreach ($haystack as $item) 
                    if (isset($item[$needle_field]) && $item[$needle_field] === $needle) 
                        return true; 
            } 
            else { 
                foreach ($haystack as $item) 
                        if (isset($item[$needle_field]) && $item[$needle_field] == $needle) 
                        return true; 
            } 
            return false; 
        }        
}